<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * 新闻公告管理
 */
class NewsController extends BaseController {

    /**
     * 新闻列表
     */
    public function newsList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');

        $title = I('post.name', '');

        //参数数组
        $params = array();
        //构造WHERE条件
        $where = 'n.status=1 ';
        if (!empty($title)) {
            $params['n.title'] = $title;
            $where .= "AND `n.title` LIKE '%{$title}%' ";
        }

        $news = M('News');
        //数据总数
        $total = $news
                ->alias('n')
                ->where($where)
                ->count();

        //数据列表
        $list = $news->field('n.*,u.name,u.phone')
                ->alias('n')
                ->where($where)
                ->join('LEFT JOIN __USERS__ u ON n.user_id=u.id')
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['add_time'] = !empty($item['add_time']) ? date('Y-m-d H:i:s', intval($item['add_time'])) : '';
                $item['upd_time'] = !empty($item['upd_time']) ? date('Y-m-d H:i:s', intval($item['upd_time'])) : '';
            }
        }

        $this->assign('pageSize', $pageSize);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('orderField', $orderField);
        $this->assign('orderDirection', $orderDirection);
        $this->assign('total', $total);
        $this->assign('list', $list);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 添加文章
     */
    public function addNews() {
        if (IS_POST) {
            $title = I('post.title', '');
            $news = M('News');

            if (empty($title)) {
                ajaxReturn('文章标题不能为空', 300);
            }

            $data = $news->create();
            if ($data) {
                $data['user_id'] = session('userInfo.id');
                $data['add_time'] = time();
                $data['upd_time'] = time();
                $ret = $news->add($data);
                if ($ret) {
                    ajaxReturn('添加文章成功', 200, '', '', '', 'Admin/News/newsList');
                } else {
                    ajaxReturn('添加文章失败', 300);
                }
            } else {
                ajaxReturn('创建数据对象失败', 300);
            }
        } else {
            $this->display();
        }
    }

    /**
     * 修改文章
     */
    public function upNews() {
        if (IS_POST) {
            $title = I('post.title', '');
            $artId = I('post.artId', 0);
            $news = M('News');

            if (empty($title)) {
                ajaxReturn('文章标题不能为空', 300);
            }

            if (empty($artId)) {
                ajaxReturn('文章ID不能为空', 300);
            }

            $data = $news->create();
            if ($data) {
                $data['upd_time'] = time();

                $ret = $news->where("id={$artId}")->save($data);
                if ($ret) {
                    ajaxReturn('修改文章成功', 200, '', '', '', 'Admin/News/newsList');
                } else {
                    ajaxReturn('修改文章失败', 300);
                }
            } else {
                ajaxReturn('创建数据对象失败', 300);
            }
        } else {
            $artId = I('get.artId', 0);

            if (empty($artId)) {
                ajaxReturn('文章ID不能为空', 300);
            }
            $news = M('News');
            $artInfo = $news->where("id={$artId}")->find();
            if (empty($artInfo)) {
                ajaxReturn('没有找到该文章信息', 300);
            }
            $this->assign('artInfo', $artInfo);
            $this->display();
        }
    }

    /**
     * 删除文章
     */
    public function delNews() {
        $ids = I('get.ids', '');
        if (empty($ids)) {
            ajaxReturn('请选择删除项', 300);
        }
        $news = M('News');
        $data = array(
            'status' => 0
        );
        $ret = $news->where("FIND_IN_SET(id,'{$ids}')")->save($data);
        if ($ret) {
            ajaxReturn('删除成功', 200, '', '', '', 'Admin/News/newsList');
        } else {
            ajaxReturn('删除失败', 300);
        }
    }

    /**
     * 上传缩略图
     */
    public function uploadThumb() {
        $info = uploadOne($_FILES['file'], array('savePath' => 'thumb/'));
        if (!$info) {
            ajaxReturn('上传失败', 300);
        } else {
            $filePath = C('UPLOAD_CONFIG.rootPath') . $info['savepath'] . $info['savename'];
            $filePath = ltrim($filePath, '.');
            ajaxReturn('上传成功', 200, array('filename' => $filePath));
        }
    }

}
